//
//  WZGAplyToShopkeeperVC.m
//  ZCJZ
//
//  Created by 潘洪波 on 2020/2/26.
//  Copyright © 2020 jeking. All rights reserved.
//

#import "WZGAplyToShopkeeperVC.h"
#import "WZGAplyToShopkeeperFirstCell.h"
#import "WZGAplyToShopkeeperSecondCell.h"
#import "WZGAplyFillInformationVC.h"
#import "ZCWebVC.h"
#import "WZGAplyIntoView.h"
#import <MJExtension.h>
@interface WZGAplyToShopkeeperVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *mTableView ;
@property (nonatomic, strong) NSMutableArray *dataArray ;
@property (nonatomic, assign) NSInteger type ;
@property (nonatomic, copy) NSArray *introArray ;

@end

@implementation WZGAplyToShopkeeperVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.mTableView.delegate = self;
    self.mTableView.dataSource = self;
    self.mTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.mTableView.estimatedRowHeight = 100;
    
    self.navTitle = @"店主申请";
    
    [self setRightItemWithTitle:@"取消" action:@selector(dismissVC)];
}

// 注销账号（退出登录）
- (void)dismissVC{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    ZCBaseUserManager *manager = [ZCBaseUserManager shareInstance];
    manager.userModel = nil;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    
    [ud setValue:nil forKey:@"userinfo"];
    [ud setValue:nil forKey:@"upassTF"];
    [ud setValue:nil forKey:@"accessToken"];
    
    [ud removeObjectForKey:@"userinfo"];
    [ud removeObjectForKey:@"upassTF"];
    [ud removeObjectForKey:@"accessToken"];
    
    [ud synchronize];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"homereload" object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"catereload" object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"mainshoppingnum" object:@(0)];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"loginSuccess" object:nil];
    
    [self dismissViewControllerAnimated:YES completion:^{
        [self.tabBarController setSelectedIndex:0];
    }];
}

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.mTableView.frame = self.view.frame;
}

#pragma mark - UITableViewDataSource
#pragma mark -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        static NSString *CellIdentifier = @"firstCell";
        WZGAplyToShopkeeperFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell = [[WZGAplyToShopkeeperFirstCell alloc] initWithFlex:nil reuseIdentifier:CellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        return cell;
    }else {
        static NSString *CellIdentifier = @"secondCell";
        WZGAplyToShopkeeperSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell = [[WZGAplyToShopkeeperSecondCell alloc] initWithFlex:nil reuseIdentifier:CellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.model = self.dataArray[indexPath.row-1];
        WeakSelf(self);
        cell.introClick = ^{
            [weakself introClick:indexPath.row-1];
        };
        return cell;
    }
}

#pragma mark - UITableViewDelegate
#pragma mark -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return;
    }
    WZGAplyModel *model = self.dataArray[indexPath.row-1];
    if (model.selected) {
        return;
    }
    model.selected = YES;
    self.type = model.type;
    for (WZGAplyModel *m in self.dataArray) {
        if (m.type == model.type) {
            continue;
        }
        m.selected = NO;
    }
    [tableView reloadData];
}

- (void)nextAction{
    switch (self.type) {
        case 1://会员申请升级  去答题
            [self loadHomework];
            break;
        case 2://大学生申请升级
            [self transferToFillInformationVC:self.type];
            break;
        case 3://残疾人申请升级
            [self transferToFillInformationVC:self.type];
            break;
        default:
            
            break;
    }
}

/// 答题
- (void)loadHomework {
    ZCWebVC *vc = [[ZCWebVC alloc] init];
    vc.navTitle = @"晋升考核";
    extern NSString *baseURLStr;
    NSString *token = [NSString stringWithFormat:@"%@",self.userModel.member.accessToken];
    NSString *url = [NSString stringWithFormat:@"%@%@?token=%@&from=app",baseURLStr,@"/h5/index.html#/home/promoteQA",token];
    vc.remotePath = url;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

/// 填写申请资料
/// @param type <#type description#>
- (void)transferToFillInformationVC:(NSInteger)type{
    WZGAplyFillInformationVC *fillVC = [WZGAplyFillInformationVC new];
    fillVC.type = type;
    fillVC.navTitle = @"提交认证";
    [self.navigationController pushViewController:fillVC animated:YES];
}

- (void)introClick:(NSInteger)index{
    [WZGAplyIntoView showIntroViewWithModel:[self.introArray objectAtIndex:index]];
}

- (NSArray*)introArray{
    NSMutableArray *dArr = [NSMutableArray array];
    if (!_introArray) {
        NSArray *arr = @[
            @{@"title":@"店主认证考试说明",
              @"des":@"通过完成平台对想认证经营小店的用户进行的考试，符合条件并通过考试即可成为店主。"},
            @{@"title":@"大学生申请说明",@"des":@"微折购平台为“应届毕业生”和“在校期间”的大学生提供开店扶持，只需提交以下材料，经平台审核无误后均可成为店主。\n 1.个人身份证正反面照片 \n2.个人手持身份证照片 \n3.大学生毕业证照片（未毕业可提供学生证，或开具在校证明）"},
            @{@"title":@"残疾人申请说明  ",
              @"des":@"微折购平台为帮助社会残障人士，特别提供开店扶持，只需提交以下材料，经平台审核无误后均可成为店主。\n1)个人身份证正反面照片\n2)个人手持身份证照片\n3)残疾人证照片或相关部门开具的残障人士证明"}];
        
        for(int i = 0 ; i < arr.count ; i++){
            NSDictionary *dict = [arr objectAtIndex:i];
            WZGAplyIntoViewModel *model = [WZGAplyIntoViewModel mj_objectWithKeyValues:dict];
            [dArr addObject:model];
        }
    }
    return dArr;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
        for(int i = 1 ; i <= 3 ; i++){
            WZGAplyModel *model = [[WZGAplyModel alloc] init];
            model.type = i;
            switch (i) {
                case 1:
                    model.title = @"店主认证考试";
                    model.content = @"通过平台对用户进行的考试";
                    break;
                case 2:
                    model.title = @"大学生申请";
                    model.content = @"为“应届生”和在校大学生提供开店扶持";
                    break;
                case 3:
                    model.title = @"残疾人申请";
                    model.content = @"为帮助社会残障人士，提供开店扶持";
                    break;
                default:
                    
                    break;
            }
            [_dataArray addObject:model];
        }

    }
    return _dataArray;
}

- (void)dealloc{
    NSLog(@"===== dealloc =====");
}
@end
